package edu.towson.cosc.cosc455.dspenc5.compiler;

import java.util.TreeMap;

public class MySemanticAnalyzer {

	/*
	 * constants for each html tag used are created below
	 */
	
    final static String DOC_BEGIN = "<html>";
    final static String DOC_END = "</html>";
    
    final static String HEAD_BEGIN = "<head>";
    final static String HEAD_END = "</head>";
    
    final static String BOLD_BEGIN = "<b>";
    final static String BOLD_END = "</b>";
    
    final static String ITALICS_BEGIN = "<i>";
    final static String ITALICS_END = "</i>";
    
    final static String COMMENT_BEGIN = "<!--";
    final static String COMMENT_END = "-->";
    
    final static String TITLE_BEGIN = "<title>";
    final static String TITLE_END = "</title>";

    final static String LIST_BEGIN = "<ul>";
    final static String LIST_END = "</ul>";
    
    final static String ITEM_START = "<li>";
    final static String ITEM_END = "</li>";
    
    final static String VARIABLE_BEGIN = "";
    final static String VARIABLE_MIDDLE = "";
    final static String VARIABLE_END = "";
    
    final static String VARIABLE_USE_BEGIN = "";
    final static String VARIABLE_USE_END = "";
    
    final static String NEWLINE = "<br>";
    
    final static String VIDEO_BEGIN = "<iframe src=\"";
    final static String VIDEO_END = "\"/>";
    
    final static String AUDIO_BEGIN = "<audio controls>\n    <source src=\"";
    final static String AUDIO_END = "\">\n</audio>";
    
    final static String PARAGRAPH_BEGIN = "<p>";
    final static String PARAGRAPH_END = "</p>";
    
  //used to match lol code tags with html tags
    public static TreeMap<String, String> conversionMap = new TreeMap<>();
    
	public static void main(String[] args) {
		
	}
	
	/**
	 * iterates through the parse tree looking up tokens gathered to see if they are a tag,
	 * if it is a tag, then its equivalent html tag replaces it in the output string,
	 * if it is not a tag then it is assumed to be text and just added as is to the html output string
	 */
	public void convertToHTML(){
		boolean inParagraph = false;//used for scoping
		for(int i = 0; i < MyCompiler.parseTree.size(); i++){
			if(conversionMap.containsKey(MyCompiler.parseTree.get(i).toUpperCase())){
				MyCompiler.htmlOutput = MyCompiler.htmlOutput + conversionMap.get(MyCompiler.parseTree.get(i).toUpperCase());
				if(MyCompiler.parseTree.get(i).toUpperCase().equals(LexicalTokens.PARAGRAPH_BEGIN)){
					inParagraph = true;
				}
				/*
				 * Below loop determines if another variable has been defined for the current paragraph using the same name as
				 * one previously used. If these cases are true then the last token is removed from the output by taking
				 * the substring of the output minus the length of the last token. Next the correct variable is found
				 * in the map and then is attached to the output string.
				 */
				if(conversionMap.containsKey(MyCompiler.parseTree.get(i).toUpperCase()+MyCompiler.parseTree.get(i).toUpperCase())&& inParagraph){
					MyCompiler.htmlOutput = MyCompiler.htmlOutput.substring(0, MyCompiler.htmlOutput.length()-conversionMap.get(MyCompiler.parseTree.get(i).toUpperCase()).length());
					MyCompiler.htmlOutput = MyCompiler.htmlOutput + conversionMap.get(MyCompiler.parseTree.get(i).toUpperCase()+MyCompiler.parseTree.get(i).toUpperCase());
				}
			}
			/*
			 * adds text from parse tree to output
			 */
			else{
				if(MyCompiler.parseTree.get(i).equals(PARAGRAPH_END)){
					inParagraph = false;
				}
				MyCompiler.htmlOutput = MyCompiler.htmlOutput + MyCompiler.parseTree.get(i);
			}
		}
	}
}
